<?php

class EmailsController extends AppController
{

	public $name = 'Emails';
	public $helpers = array();
	public $uses = array('SendMail','Email');

	/* @var $SendMail SendMail */

	function beforeFilter()
	{
		parent::beforeFilter();

		$this->modelName = Inflector::singularize($this->name);
		$this->po_domain = Inflector::underscore($this->name);
		$this->url_part  = 'emails-queue';
		$this->set('po_domain', $this->po_domain);
		$this->set('url_part',  $this->url_part);
		$this->set('modelName', $this->modelName);
		$this->set('PKName',    $this->{$this->modelName}->primaryKey);

	}

	function sends()
	{
		Auth::Access("*");
		$this->SendMail->send_queue();
		exit();
	}

	function list_of()
	{
		Auth::Access('active & admin');

	    $modelName = $this->modelName;
		$PKName    = $this->{$modelName}->primaryKey;
		$po_domain = $this->po_domain;
		$url_part  = $this->url_part;

		$this->pageTitle = strip_tags(__d($po_domain, 'list_title', true));

		////// MENU ///////////////////////
	    $this->MainMenu->setActive($this->MainMenu->findItem("/sending-log/"));
		///////////////////////////////////

		vendor('contingencies');
		vendor('order_html');
		vendor('pagination_html');
		vendor('page_of_data');

		$contingencies = new Contingencies();
        $contingencies->model =& $this->{$modelName};

        /////////////////////////////////////////////////////////////
        $ses_war_name = __CLASS__.':'.__FUNCTION__.':filter:1';
        if (!empty($this->data['Filter'])) {
            if (!empty($this->data['Filter']['submit_reset'])) {
                unset($_SESSION[$ses_war_name]);
                unset($this->data['Filter']);
            } elseif (!empty($this->data['Filter']['submit_apply'])) {
                $_SESSION[$ses_war_name] = $this->data['Filter'];
            }
        }
        $filter = isset($_SESSION[$ses_war_name]) ? $_SESSION[$ses_war_name] : array();
        if (empty($this->data['Filter'])) {
            $this->data['Filter'] = $filter;
        }
        $this->set('filter', $filter);
        /////////////////////////////////////////////////////////////
		$conds = array();
		////////////////////////////////////////////////////////////////////////
		if (!empty($filter['from_date'])) {
			$conds[]["`{$modelName}`.`created`"] = ">={$filter['from_date']}";
		}
		if (!empty($filter['to_date'])) {
			$conds[]["`{$modelName}`.`created`"] = "<=".Date::getToDateTime($filter['to_date']);
		}
		$contingencies->conditions =& $conds;
		////////////////////////////////////////////////////////////////////////

        $appearence = new OrderHTML();
        $appearence->orderFields = array(
            $PKName => "`{$modelName}`.`{$PKName}`",
            'created' => "`{$modelName}`.`created`",
            'retries' => "`{$modelName}`.`retries`",
        );
        $appearence->defaultOrder = Order::parseString('~created');
        $appearence->processParams();

        $pagination = new PaginationHTML();
        $pagination->defaultLimit = 20;
        $pagination->processParams();

        $list = new PageOfData($contingencies, $appearence, $pagination);

        vendor('mail/mail_model');

        foreach ($list->rows as &$row) {
            try {
                $row[$modelName]['data'] = unserialize($row[$modelName]['data']);
            } catch (Exception $e) {}
        }

		$this->set('list', $list);
	}
}

?>